from urllib.request import urlopen
from bs4 import BeautifulSoup
#  抓取小说房客
arr = []
zfc="https://www.rzlib.net/b/2/2493/"
html = urlopen("https://www.rzlib.net/b/2/2493/")
bsObj = BeautifulSoup(html, "html.parser")
firstChapter = bsObj.find_all('ul', attrs={"class": "ListRow"})
for v in range(0,862):
    a = list(firstChapter[v])
    for i in range(1,8,2):
        b=a[i]
        c = str(b)
        d = c[13:25]
        arr.append(zfc+d)
for num in range(1402, 3448):
    print("正在获取第"+str(num+1)+"个链接")
    html2 = urlopen(arr[num])
    aa = BeautifulSoup(html2, "html.parser")
    firstChapter21 = aa.find_all('h1')
    firstChapter22 = aa.find_all('div', attrs={"id": "chapter_content"})
    print("开始写入内容"+str(num+1)+"的链接内容")
    cc=str(firstChapter22[0].get_text())
    dd=str(firstChapter21[0].get_text())
    with open(r"F:\PhyWorkSpeace\bbb.txt", "a") as file:
        file.write(dd.replace(u'\xa0', u' ') + '\n')
        file.write(cc.replace(u'\xa0', u' ') + '\n')
    print("写入完第 "+str(num+1)+" 章")
print("全部完成")
# for m in arr:
#     print(m)
#     file.write("http://www.jianshu.com" + firstChapter.get('href') + '\n\n')
